<template>
  <div>
    <el-table border style="width: 100%;margin-top:10px" :data="records">
      <el-table-column type="index" label="序号" align="center"></el-table-column>
      <el-table-column prop="skuName" label="名称" align="center"></el-table-column>
      <el-table-column prop="skuDesc" label="描述" align="center"></el-table-column>
      <el-table-column label="默认图片" align="center">
        <template slot-scope="{row}">
          <img :src="row.skuDefaultImg" style="width:100px">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" align="center"></el-table-column>
      <el-table-column prop="price" label="价格" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="{row}">
          <!-- isSale为0表示上架 isSale为1表示下架 -->
          <el-button type="success" title="已下架" plain size="mini" v-if="row.isSale == 0" @click="sale(row)">上架
          </el-button>
          <el-button type="success" title="已上架" v-else plain size="mini" @click="cancel(row)">下架</el-button>

          <el-button icon="el-icon-edit" type="primary" plain size="mini" @click="$message('正在开发中')"></el-button>
          <el-button icon="el-icon-info" type="info" plain size="mini" @click="details(row)"></el-button>
          <el-button icon="el-icon-delete" type="danger" plain size="mini"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页器 -->
    <!-- 
      page-size每页显示多少条数据 total总条数
      current-page当前是第几页
      page-sizes每页显示个数选择器的选项设置 
      layout组件布局
      size-change：每页显示的条数改变时会触发
      current-change：页码发生改变时触发
     -->
    <el-pagination align="center" style="margin-top:10px" @current-change="currentChange" @size-change="sizeChange"
      :page-sizes="[3, 5, 10, 100]" :current-page="page" layout="prev, pager, next, jumper,->,sizes,total"
      :total="total" background :page-size="limit">
    </el-pagination>

    <!-- 抽屉 -->
    <el-drawer :visible.sync="show" :before-close="handleClose" close-on-press-escape :show-close="false" size="30%">
      <div v-loading="loading">
          <!-- row就是一行 -->
          <el-row>
            <!-- span	栅格占据的列数 -->
            <el-col :span="5">名称：</el-col>
            <el-col :span="16">{{ skuInfo.skuName }}</el-col>
          </el-row>
          <el-row>
            <el-col :span="5">描述：</el-col>
            <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
          </el-row>
          <el-row>
            <el-col :span="5">价格：</el-col>
            <el-col :span="16">{{ skuInfo.price }}元</el-col>
          </el-row>
          <el-row>
            <el-col :span="5">平台属性：</el-col>
            <el-col :span="16">
              <el-tag type="primary" size="normal" v-for="attr in skuInfo.skuAttrValueList" :key="attr.id"
                style="margin-right:5px">{{ attr.attrId }}-{{ attr.valueId }}
              </el-tag>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="5">商品图片：</el-col>
            <el-col :span="16" style="border:1px solid rgb(211 217 217)">
              <el-carousel type="card" trigger="click" height="150px">
                <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
                  <img :src="item.imgUrl" width="100%">
                </el-carousel-item>
              </el-carousel>
            </el-col>
          </el-row>
        </div>
      </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'Sku',
  data() {
    return {
      // 初始化参数，带给服务器
      page: 1,//当前是第几页
      limit: 10,//页面显示多少条数据
      records: [],//存储服务器响应的sku数据，用于渲染页面
      total: 0,//存储服务器响应的分页器一共显示多少条数据
      show: false,//控制抽屉的显示和隐藏
      skuInfo: [],//存储服务器响应的数据，用于渲染抽屉页面的数据
      loading:false,//加载效果
    }
  },
  mounted() {
    this.getSkuList()
  },
  methods: {
    // 获取sku列表数据
    async getSkuList() {
      const { page, limit } = this
      const result = await this.$API.sku.reqGetSkuList(page, limit);
      console.log(result);
      if (result.code == 200) {
        this.records = result.data.records
        this.total = result.data.total
      }
    },
    // 分页器-每页显示的条数发生改变时
    sizeChange(limit) {
      this.limit = limit
      this.getSkuList()
    },
    // 分页器-页码发生改变时
    currentChange(pages) {
      this.page = pages
      this.getSkuList()
    },
    // 上架
    async sale(row) {
      try {
        let result = await this.$API.sku.reqSale(row.id)////要上架的id
        if (result.code == 200) {
          row.isSale = 1  //1为上架
          this.getSkuList()
          this.$message({ type: 'success', message: '上架成功' })
        }
      } catch (error) {
        console.log(error);
      }
    },
    // 下架
    async cancel(row) {
      try {
        const result = await this.$API.sku.reqSkuById(row.id)//要下架的id
        if (result.code == 200) {
          this.isSale = 1 //1为下架
          this.getSkuList()
          this.$message({ type: 'success', message: '下架成功' })
        }
      } catch (error) {
        console.log(error);
      }

    },
    // 详情  获取SKU详情
    async details(row) {
      // 打开抽屉
      this.show = true
      // 打开加载效果
      this.loading=true

      try {
        const result = await this.$API.sku.reqGetSkuDetails(row.id)
        if (result.code == 200) {
          this.skuInfo = result.data
          // 关闭加载效果
          this.loading=false
        }
      } catch (error) {//捕获异步任务Promise状态为失败的回调
        console.log(error);
      }
    },
    // 关闭抽屉
    handleClose(done) {
      // 清空skuInfo数据
      this.skuInfo=[]
      done()
    }
  }
}
</script>

<!-- 轮播图 -->
<style>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 150px;
    margin: 0;
  }

  .el-carousel__item:nth-child(2n) {
     background-color: #99a9bf;
  }
  
  .el-carousel__item:nth-child(2n+1) {
     background-color: #d3dce6;
  }
</style>

<style scoped>
   .el-row .el-col-5{
      font-size:18px;
      text-align:right;
      font-weight: bold;
   }
   .el-col{
     margin:10px 10px;
   }

   /* 深度选择器:用于样式穿透

>>>  一般用于原生CSS

/deep/ 一般用于less

 ::v-deep 一般用户scss */

   /* >>>.el-carousel__button{
    width:10px;
    height:10px;
    background:red;
    border-radius:50%;
  } */
</style>